package com.ktjy.ch15.service.impl;

import com.ktjy.ch15.entity.SysUser;
import com.ktjy.ch15.mapper.SysUserMapper;
import com.ktjy.ch15.service.SysUserService;
import com.ktjy.ch15.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public SysUser findByPassword(String oldPassword) {
        return sysUserMapper.getByPassword(oldPassword);
    }

    @Override
    public SysUser login(String account, String password) {
        SysUser sysUser = sysUserMapper.getByAccount(account);
        if(sysUser != null){
            if(sysUser.getPassword().equals(password))
                return sysUser;
        }
        return null;
    }

    @Override
    public Page<SysUser> findPage(SysUser sysUser, Integer pageNo, Integer pageSize) {
        Page<SysUser> page = new Page<SysUser>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 查询总数，计算总页数
        Integer count = sysUserMapper.count(sysUser);
        page.setTotalCount(count);
        // 查询分页数据
        Integer pageBegin=(pageNo-1)*pageSize;
        List<SysUser> data = sysUserMapper.getPage(sysUser, pageBegin, pageSize);
        page.setData(data);
        return page;
    }

    @Override
    public boolean save(SysUser sysUser) {
        return sysUserMapper.insert(sysUser) > 0;
    }

    @Override
    public SysUser findById(Long id) {
        return sysUserMapper.getById(id);
    }

    @Override
    public boolean update(SysUser sysUser) {
        return sysUserMapper.update(sysUser) > 0;
    }

    @Override
    public boolean delete(Long id) {
        return sysUserMapper.delete(id)> 0;
    }

    @Override
    public SysUser findByAccount(String account) {
        return sysUserMapper.getByAccount(account);
    }
}
